
export function format(json) {
    let key = Date.now();
    let minX = Infinity;
    let maxX = -Infinity;
    let minY = Infinity;
    let maxY = -Infinity;
    const sortList = [];
    const obj = {};
  
    let pipeWidth = 8;
    for (let info of json.pipes) {
      const fromCoord = info.from;
      const toCoord = info.to;
  
      key++;
      const left = Math.min(fromCoord[0], toCoord[0]);
      const top = Math.min(fromCoord[1], toCoord[1]);
      minX = Math.min(left, minX);
      maxX = Math.max(fromCoord[0], toCoord[0], maxX);
      minY = Math.min(top, minY);
      maxY = Math.max(fromCoord[1], toCoord[1], maxY);
      const id = key.toString();
      sortList.push(id);
      obj[key] = {
        value: "Line",
        id,
        lineWidth: pipeWidth,
        isModules: true,
        lineColor: info.color[0],
        lineColor1: info.color[1],
        lineType: "solid",
        hideTrack: "false",
        borderRadius: "0",
        left,
        top,
        width: Math.abs(Math.round(toCoord[0] - fromCoord[0])) || pipeWidth,
        height: Math.abs(Math.round(toCoord[1] - fromCoord[1])) || pipeWidth,
        track: {
          startPosition: {
            x: fromCoord[0] - left,
            y: fromCoord[1] - top,
          },
          endPosition: {
            x: toCoord[0] - left,
            y: toCoord[1] - top,
          },
        },
      };
    }
    const teeWidth = (24 / 57) * 30;
  
    json.tee.map((val) => {
      const { type, pos, color, hflip, vflip } = val;
      let width = 30;
      let height = 30;
      let left = Math.round(pos?.[0] - width / 2);
      let top = Math.round(pos?.[1] - height / 2);
      key++;
      const id = key.toString();
      sortList.push(id);
      let rotate = 0;
      const pipeColor = color[0];
      const pipeColor2 = color[1];
  
      if (type === "tee") {
        if (hflip) {
          rotate += 180;
        }
        height = Math.round((width / 57) * 90);
        top = Math.round(pos?.[1] - height / 2);
        switch (rotate) {
          case 90:
            top = pos?.[1];
            top = Math.round(pos?.[1] - width - 5 / 2);
            break;
          case 180:
            left = Math.round(pos?.[0] - teeWidth / 2);
            break;
          case 270:
            top = Math.round(pos?.[1] - width / 2);
            break;
          default:
            left = Math.round(pos?.[0] - width + teeWidth / 2);
            break;
        }
      } else {
        if (hflip) {
          rotate = 90;
        }
        if (vflip) {
          rotate = 270;
        }
        switch (rotate) {
          case 90:
            left = Math.round(pos?.[0] - teeWidth / 2);
            top = Math.round(pos?.[1] - height + teeWidth / 2);
            break;
          case 180:
            top = Math.round(pos?.[1] - teeWidth / 2);
            left = Math.round(pos?.[0] - teeWidth / 2);
            break;
          case 270:
            top = Math.round(pos?.[1] - teeWidth / 2);
            left = Math.round(pos?.[0] - width + teeWidth / 2);
            break;
          default:
            left = Math.round(pos?.[0] - width + teeWidth / 2);
            top = Math.round(pos?.[1] - height + teeWidth / 2);
            break;
        }
      }
      obj[key] = {
        value: "Pipe",
        isModules: true,
        type: type === "tee" ? "tee" : "right",
        pipeColor,
        pipeColor2,
        width,
        height,
        left,
        top,
        rotate,
        id,
      };
    });
  
    const pointWidth = 110;
    const pointHeight = 35;
  
    json.machine.map((val) => {
      const { aabb, info, sn, pointList } = val;
      const width = Math.round(aabb?.max?.[0] - aabb?.min?.[0]);
      const height = Math.round(aabb?.max?.[1] - aabb?.min?.[1]);
      const left = Math.round(aabb?.min?.[0]);
      const top = Math.round(aabb?.min?.[1]);
      minX = Math.min(left, minX);
      maxX = Math.max(left + width, maxX);
      minY = Math.min(top, minY);
      maxY = Math.max(top + height, maxY);
      key++;
      const id = key.toString();
      sortList.push(id);
      let imageWidth = 20;
      let imageHeight = 40;
      switch (info.match) {
        case "manometer":
          imageWidth = 20;
          imageHeight = 40;
          obj[key] = {
            value: "Image",
            id,
            isModules: true,
            width: imageWidth,
            height: imageHeight,
            image: {
              url: "https://static-dist-pub.oss-cn-hangzhou.aliyuncs.com/组态库/图片1702633347757/压力传感器.png",
            },
            bgLayout: "contain",
            left,
            top: Math.round(top),
          };
          break;
        case "thermometer":
          imageWidth = 30;
          imageHeight = 35;
          obj[key] = {
            value: "Image",
            id,
            isModules: true,
            width: imageWidth,
            height: imageHeight,
            image: {
              url: "https://static-dist-pub.oss-cn-hangzhou.aliyuncs.com/组态库/图片1702632625458/温度传感器.png",
            },
            bgLayout: "contain",
            left,
            top: Math.round(top),
          };
          break;
        case "calorimeter":
          imageWidth = 40;
          imageHeight = 40;
          obj[key] = {
            value: "Image",
            id,
            isModules: true,
            width: imageWidth,
            height: imageHeight,
            image: {
              url: "https://static-dist-pub.oss-cn-hangzhou.aliyuncs.com/组态库/图片1702627902327/热量表.png",
            },
            bgLayout: "contain",
            left,
            top: Math.round(top),
          };
          break;
        default:
          obj[key] = {
            value: "Point",
            id,
            isModules: true,
            pointData: {
              pointType: 0,
              pointName: info.name,
              nid: sn,
              deviceType: info.match?.replace("/", "_"),
            },
            width,
            height,
            left,
            top,
            contentType: ["icon"],
          };
          break;
      }
      pointList?.map((point) => {
        key++;
        const left = Math.round(point.pos[0] - pointWidth / 2);
        const top = Math.round(point.pos[1] - pointHeight / 2);
        minX = Math.min(left, minX);
        maxX = Math.max(left + pointWidth, maxX);
        minY = Math.min(top, minY);
        maxY = Math.max(top + pointHeight, maxY);
        const id = key.toString();
        sortList.push(id);
        obj[key] = {
          value: "Point",
          id,
          isModules: true,
          pointData: {
            pointType: 1,
            pointName: point.cnName,
            enName: point.enName,
            nid: sn,
            deviceName: info.name,
          },
          width: pointWidth,
          height: pointHeight,
          left,
          top,
          contentType: ["text"],
          dataFontSize: "0",
          labelColor: "#080808",
          labelFontWeight: "normal",
          labelFontSize: "20",
        };
      });
    });
    const moveX = Math.max(0, -minX);
    const moveY = Math.max(0, -minY);
    if (moveX || moveY) {
      sortList.map((val) => {
        const node = obj[val];
        if (node.value === "Line") {
          obj[val] = {
            ...node,
            track: {
              startPosition: {
                x: node.track.startPosition.x + moveX,
                y: node.track.startPosition.y + moveY,
              },
              endPosition: {
                x: node.track.endPosition.x + moveX,
                y: node.track.endPosition.y + moveY,
              },
            },
          };
        } else {
          obj[val] = {
            ...node,
            left: node.left + moveX,
            top: node.top + moveY,
          };
        }
      });
    }
    obj.sort = sortList;
    obj.bg = {
      image: {
        width: maxX + moveX + 100,
        height: maxY + moveY + 100,
      },
    };
    return obj;
  }
  
  // const newJson2 = format(newJson);
  // fs.writeFileSync(
  //   "newJson.json",
  //   JSON.stringify({
  //     source: "3d",
  //     option: {
  //       pc: newJson2,
  //     },
  //   })
  // );
  